class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        int n = nums.size()*2-1;
        int n1 = nums.size();
        vector<int> temp;
        vector<int> ans(n1,-1);
        for(int i = 0;i<n;i++)
        {
            while(!temp.empty() && nums[i%n1] > nums[temp.back()])
            {
                ans[temp.back()] = nums[i%n1];
                temp.pop_back();
            }
            temp.push_back(i%n1);
        }
        return ans;
    }
};